package com.webapp.model;

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

/**
 * LoginModel is the nuts and bolts of the login.jsp! This holds all the authetication logic
 * and creates a User Object for the Servlet to store into the HTTPSESSION!
 * @author Team GNK
 */
public class LoginModel {
	
	private static Connection conn = null;
	private static String driver = "com.mysql.jdbc.Driver";
	private static String dbUrl = "jdbc:mysql://mysql1.cs.stonybrook.edu:3306/gdonnelly";
	private static String dbPassword = "106338300";
	private static String dbUser = "gdonnelly";
	
	private User theUserObj;
	private String thePassword;
	private String theType;
	private String theUser;
	
	private String theCountry;
	private int numAssgnReviews;
	private String researchArea;
	private String theEmail;
	
	
	/**
	 *  Check user authetication and if the authetication passes create a user 
	 *  object with the users data!
	 *  @param user
	 *  @param password
	 *  @return boolean
	 **/
	public String checkAuthentication(String user, String password) {
		try{
			Statement stmt = connect();
			ResultSet rs;
			
			rs = stmt.executeQuery("select * from user where user_name = '" +user+ "' and password = '" +password+"'");
			
			//check if valid
			if(rs.next()){
				//set values to "theUserObj"
				thePassword = rs.getString(2);
				theUser = rs.getString(1);
				theType = rs.getString(9);
				
				theCountry = rs.getString(4);
				researchArea = rs.getString(5);
				numAssgnReviews = rs.getInt(6);
				theEmail = rs.getString(3);
				
				

				//close the connection
				rs.close();
				stmt.close();
				conn.close();
				
				return "true";
			}
			else
				return "false";
		}
		catch(Exception ex){
			System.out.println("Could not connect to database");
			return "error";
		}
		
		
	}
	
	/**
	 * Put the data in the user object 
	 * @return User 
	 **/
	public User getUserData(String name) {
		theUserObj = new User();
		theUserObj.setType(theType);
		theUserObj.setUserName(theUser);
		theUserObj.setCountry(theCountry);
		theUserObj.setResearch(researchArea);
		theUserObj.setReviews(numAssgnReviews);
		theUserObj.setEmail(theEmail);
		
		//creates a vector of App_Id's the user needs to review
		try{
			Statement stmt = connect();
			ResultSet rs;
		
			//get user email address
			rs = stmt.executeQuery("SELECT * FROM user WHERE user_name  = '" +name+ "'");
			rs.next();
			String userEmail = rs.getString(3);
			
			ArrayList<Integer> reviews = new ArrayList<Integer>();
			
			rs = stmt.executeQuery("SELECT * FROM user_needs_review WHERE user_email = '" +userEmail+ "'");
			while(rs.next()){
				reviews.add(rs.getInt(3));
			}
			
			theUserObj.setReviewsNeeded(reviews);
			
			
		}catch(Exception ex){/*cannot connect*/}
		
		return theUserObj;
	}
	
	public static Statement connect(){
		try{
			Class.forName(driver);
			conn = (Connection) DriverManager.getConnection(dbUrl, dbUser, dbPassword);
			Statement stmt = (Statement) conn.createStatement();
			
			return stmt;
		}catch(Exception ex){return null;}
	}

}
